package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	/**
	 * ...
	 * @author ...
	 */
	public class ParticleManager extends MovieClip
	{
		public var particulas: Array = new Array()
		private var primeirasCriada : Boolean = false;
		
		public function ParticleManager() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
			
		private function init(Evento:Event):void
		{
			if (!primeirasCriada)
			{
				make();
				make();
				make();
				make();
				make();
				make();
				make();
				make();
				make();
				make();
				make();
				make();
				make();
				make();
				primeirasCriada = false;
			}
		}
		
		public function update()
		{
			if (particulas.length)
			{
				for (var i: int = 0; i < particulas.length; i++)
				{
					if (particulas[i].elementalState == ElementalState.FOGO)
					{
						particulas[i].y -= Main.randomNumber(0.25, 0.4);
						particulas[i].alpha -= Main.randomNumber(0.018, 0.025);
						particulas[i].scaleX = particulas[i].scaleY -= 0.001;
					}
					else if (particulas[i].elementalState == ElementalState.AGUA)
					{
						particulas[i].y -= Main.randomNumber(0.15, 0.25);
						particulas[i].alpha -= Main.randomNumber(0.018, 0.03);
						particulas[i].scaleX = particulas[i].scaleY += 0.001;
					}
					else if (particulas[i].elementalState == ElementalState.TERRA)
					{
						particulas[i].y -= Main.randomNumber(0.05, 0.1);
						particulas[i].alpha -= Main.randomNumber(0.018, 0.025);
						particulas[i].scaleX = particulas[i].scaleY -= 0.001;
					}
					else
					{
						particulas[i].y -= Main.randomNumber(0.15, 0.3);
						particulas[i].alpha -= Main.randomNumber(0.008, 0.015);
						particulas[i].scaleX = particulas[i].scaleY -= 0.001;
						particulas[i].rotation += 7;
					}
					
					
					
					if (particulas[i].alpha <= 0)
					{
						destroyParticle(i);
					}
				}
			}
		}
		
		public function make()
		{
			var particulaSolitaria: MovieClip = new Particula();
			particulaSolitaria.elementalState = Main.instance.personagem.elementalState;
			particulaSolitaria.gotoAndStop(particulaSolitaria.elementalState.label);
			if (particulaSolitaria.elementalState == ElementalState.AR) { particulaSolitaria.rotation = Main.randomNumber(0, 360); }
			if (particulaSolitaria.elementalState == ElementalState.TERRA) { particulaSolitaria.alpha = Main.randomNumber(0.4, 0.6);}
			particulaSolitaria.scaleX = particulaSolitaria.scaleY = 0.07;
			particulaSolitaria.x = Main.randomNumber(Main.instance.personagem.x - 10, Main.instance.personagem.x + 10); 
			particulaSolitaria.y = Main.randomNumber(Main.instance.personagem.y - 10, Main.instance.personagem.y + 10); 
			Main.instance.tela.addChild(particulaSolitaria);
			particulas.push(particulaSolitaria);
		}
		
		private function destroyParticle(index: int):void 
		{
			Main.instance.tela.removeChild(particulas[index]);
			particulas.splice(index, 1)
			make();
		}
		
	}

}